using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Windows.Media.Imaging;
using System.Xml.Linq;
namespace INTEX.DataBase.CE
{
    public partial class Mapper
    {
        private List<VKPOS_EXT> MapVKPOS_EXT(XElement item2, bool mergedb)
        {
            List<VKPOS_EXT> result = new List<VKPOS_EXT>();
            var item4 = from item3 in item2.Elements()
                        select new VKPOS_EXT
                        {  
                            MAINKEY = (string)item3.Element("MAINKEY"),
                            VKPMANDANT = (string)item3.Element("VKPMANDANT"),
                            VKPAUFKRS = (string)item3.Element("VKPAUFKRS"),
                            VKPAUFLFD = (string)item3.Element("VKPAUFLFD"),
                            VKPAUFPOS = (string)item3.Element("VKPAUFPOS"),
                            VKPNEUBEN = (string)item3.Element("VKPNEUBEN"),
                            VKPNEUDAT = (string)item3.Element("VKPNEUDAT"),
                            VKPKUNDNR = (string)item3.Element("VKPKUNDNR"),
                            VKPARTST = (string)item3.Element("VKPARTST"),
                            VKPKOLL = (string)item3.Element("VKPKOLL"),
                            VKPART = (string)item3.Element("VKPART"),
                            VKPFARBE = (string)item3.Element("VKPFARBE"),
                            VKPPREIS = (string)item3.Element("VKPPREIS"),
                            VKPMEEH = (string)item3.Element("VKPMEEH"),
                            VKPLIEFTERM = (string)item3.Element("VKPLIEFTERM"),
                            VKPMENGE1 = (string)item3.Element("VKPMENGE1"),
                            VKPPOSWERT = (string)item3.Element("VKPPOSWERT"),
                            Photo = (string)item3.Element("Photo")
                        }; 
                        
          result = item4.ToList();
          if (mergedb)
            {                  
               var rowsexist = from u in LocalDB.VKPOS_EXT.ToList().AsQueryable()
                                           join uc in result.AsQueryable() on  u.MAINKEY equals  uc.MAINKEY
                                           select u;
                LocalDB.VKPOS_EXT.DeleteAllOnSubmit(rowsexist);
                LocalDB.SubmitChanges();
                LocalDB.VKPOS_EXT.InsertAllOnSubmit(result);
                LocalDB.SubmitChanges();
            }
        
            return result;
        }
    }
    [Table(Name = "VKPOS_EXT")]
    public class VKPOS_EXT : INotifyPropertyChanged,INotifyPropertyChanging
    {
        private string _MAINKEY="0";
        [Column(IsPrimaryKey = true, CanBeNull = false)]
        public string MAINKEY
        {
            get
            {
                return _MAINKEY;
            }
            set
            {
                if (value != _MAINKEY)
                {
                    NotifyPropertyChanging("MAINKEY");
                     _MAINKEY = value;
                     NotifyPropertyChanged("MAINKEY");
                }
            }
        }
        [Column(IsVersion = true)]
        private Binary _version;
   
        private string  _VKPMANDANT="0";
        [Column]
        public string VKPMANDANT
        {
            get
            {
                return _VKPMANDANT;
            }
            set
            {
                if (value != _VKPMANDANT)
                {
                    NotifyPropertyChanging("VKPMANDANT");
                     _VKPMANDANT = value;
                    NotifyPropertyChanged("VKPMANDANT");
                }
            }
        }
        private string  _VKPAUFKRS="0";
        [Column]
        public string VKPAUFKRS
        {
            get
            {
                return _VKPAUFKRS;
            }
            set
            {
                if (value != _VKPAUFKRS)
                {
                    NotifyPropertyChanging("VKPAUFKRS");
                     _VKPAUFKRS = value;
                    NotifyPropertyChanged("VKPAUFKRS");
                }
            }
        }
        private string  _VKPAUFLFD="0";
        [Column]
        public string VKPAUFLFD
        {
            get
            {
                return _VKPAUFLFD;
            }
            set
            {
                if (value != _VKPAUFLFD)
                {
                    NotifyPropertyChanging("VKPAUFLFD");
                     _VKPAUFLFD = value;
                    NotifyPropertyChanged("VKPAUFLFD");
                }
            }
        }
        private string  _VKPAUFPOS="0";
        [Column]
        public string VKPAUFPOS
        {
            get
            {
                return _VKPAUFPOS;
            }
            set
            {
                if (value != _VKPAUFPOS)
                {
                    NotifyPropertyChanging("VKPAUFPOS");
                     _VKPAUFPOS = value;
                    NotifyPropertyChanged("VKPAUFPOS");
                }
            }
        }
        private string  _VKPNEUBEN="0";
        [Column]
        public string VKPNEUBEN
        {
            get
            {
                return _VKPNEUBEN;
            }
            set
            {
                if (value != _VKPNEUBEN)
                {
                    NotifyPropertyChanging("VKPNEUBEN");
                     _VKPNEUBEN = value;
                    NotifyPropertyChanged("VKPNEUBEN");
                }
            }
        }
        private string  _VKPNEUDAT="0";
        [Column]
        public string VKPNEUDAT
        {
            get
            {
                return _VKPNEUDAT;
            }
            set
            {
                if (value != _VKPNEUDAT)
                {
                    NotifyPropertyChanging("VKPNEUDAT");
                     _VKPNEUDAT = value;
                    NotifyPropertyChanged("VKPNEUDAT");
                }
            }
        }
        private string  _VKPKUNDNR="0";
        [Column]
        public string VKPKUNDNR
        {
            get
            {
                return _VKPKUNDNR;
            }
            set
            {
                if (value != _VKPKUNDNR)
                {
                    NotifyPropertyChanging("VKPKUNDNR");
                     _VKPKUNDNR = value;
                    NotifyPropertyChanged("VKPKUNDNR");
                }
            }
        }
        private string  _VKPARTST="0";
        [Column]
        public string VKPARTST
        {
            get
            {
                return _VKPARTST;
            }
            set
            {
                if (value != _VKPARTST)
                {
                    NotifyPropertyChanging("VKPARTST");
                     _VKPARTST = value;
                    NotifyPropertyChanged("VKPARTST");
                }
            }
        }
        private string  _VKPKOLL="0";
        [Column]
        public string VKPKOLL
        {
            get
            {
                return _VKPKOLL;
            }
            set
            {
                if (value != _VKPKOLL)
                {
                    NotifyPropertyChanging("VKPKOLL");
                     _VKPKOLL = value;
                    NotifyPropertyChanged("VKPKOLL");
                }
            }
        }
        private string  _VKPART="0";
        [Column]
        public string VKPART
        {
            get
            {
                return _VKPART;
            }
            set
            {
                if (value != _VKPART)
                {
                    NotifyPropertyChanging("VKPART");
                     _VKPART = value;
                    NotifyPropertyChanged("VKPART");
                }
            }
        }
        private string  _VKPFARBE="0";
        [Column]
        public string VKPFARBE
        {
            get
            {
                return _VKPFARBE;
            }
            set
            {
                if (value != _VKPFARBE)
                {
                    NotifyPropertyChanging("VKPFARBE");
                     _VKPFARBE = value;
                    NotifyPropertyChanged("VKPFARBE");
                }
            }
        }
        private string  _VKPPREIS="0";
        [Column]
        public string VKPPREIS
        {
            get
            {
                return _VKPPREIS;
            }
            set
            {
                if (value != _VKPPREIS)
                {
                    NotifyPropertyChanging("VKPPREIS");
                     _VKPPREIS = value;
                    NotifyPropertyChanged("VKPPREIS");
                }
            }
        }
        private string  _VKPMEEH="0";
        [Column]
        public string VKPMEEH
        {
            get
            {
                return _VKPMEEH;
            }
            set
            {
                if (value != _VKPMEEH)
                {
                    NotifyPropertyChanging("VKPMEEH");
                     _VKPMEEH = value;
                    NotifyPropertyChanged("VKPMEEH");
                }
            }
        }
        private string  _VKPLIEFTERM="0";
        [Column]
        public string VKPLIEFTERM
        {
            get
            {
                return _VKPLIEFTERM;
            }
            set
            {
                if (value != _VKPLIEFTERM)
                {
                    NotifyPropertyChanging("VKPLIEFTERM");
                     _VKPLIEFTERM = value;
                    NotifyPropertyChanged("VKPLIEFTERM");
                }
            }
        }
        private string  _VKPMENGE1="0";
        [Column]
        public string VKPMENGE1
        {
            get
            {
                return _VKPMENGE1;
            }
            set
            {
                if (value != _VKPMENGE1)
                {
                    NotifyPropertyChanging("VKPMENGE1");
                     _VKPMENGE1 = value;
                    NotifyPropertyChanged("VKPMENGE1");
                }
            }
        }
        private string  _VKPPOSWERT="0";
        [Column]
        public string VKPPOSWERT
        {
            get
            {
                return _VKPPOSWERT;
            }
            set
            {
                if (value != _VKPPOSWERT)
                {
                    NotifyPropertyChanging("VKPPOSWERT");
                     _VKPPOSWERT = value;
                    NotifyPropertyChanged("VKPPOSWERT");
                }
            }
        }
      public void ConvertToFile(IsolatedStorageFile ISO,ObservableCollection<VKPOS_EXT> ListInput, string filep = "", string setguid="")
        {           
            try
            {
                string localfile = filep;
                if (filep.Trim() == "")
                {
                    if (setguid.Trim() == "")
                        setguid = Guid.NewGuid().ToString();
                    localfile = "Data\\PhoneDB\\VKPOS_EXT\\VKPOS_EXT" +setguid + ".xml";
                }
                string folder = "Data\\PhoneDB\\VKPOS_EXT" ;
                if (!ISO.DirectoryExists(folder))
                {
                    ISO.CreateDirectory(folder);
                }
                ISO.DeleteFile(localfile);
                StreamWriter sw = new StreamWriter(ISO.CreateFile(localfile));
                XDocument xdoc = new XDocument();
                //Root Element
                XElement root = new XElement("VKPOS_EXT");
                if (setguid.Trim() != "")
                {
                    root = new XElement("ITX"+setguid);
                }
                //Add root to file
                xdoc.Add(root);
                foreach (VKPOS_EXT row in ListInput)
                {
                    XElement f = new XElement("VKPOS_EXT");
                    XElement MAINKEY = new XElement("MAINKEY");
                     f.Add(MAINKEY);
                   
                        XElement VKPMANDANT = new XElement("VKPMANDANT");
                         VKPMANDANT.Value = row.VKPMANDANT;
                          f.Add(VKPMANDANT);
                        XElement VKPAUFKRS = new XElement("VKPAUFKRS");
                         VKPAUFKRS.Value = row.VKPAUFKRS;
                          f.Add(VKPAUFKRS);
                        XElement VKPAUFLFD = new XElement("VKPAUFLFD");
                         VKPAUFLFD.Value = row.VKPAUFLFD;
                          f.Add(VKPAUFLFD);
                        XElement VKPAUFPOS = new XElement("VKPAUFPOS");
                         VKPAUFPOS.Value = row.VKPAUFPOS;
                          f.Add(VKPAUFPOS);
                        XElement VKPNEUBEN = new XElement("VKPNEUBEN");
                         VKPNEUBEN.Value = row.VKPNEUBEN;
                          f.Add(VKPNEUBEN);
                        XElement VKPNEUDAT = new XElement("VKPNEUDAT");
                         VKPNEUDAT.Value = row.VKPNEUDAT;
                          f.Add(VKPNEUDAT);
                        XElement VKPKUNDNR = new XElement("VKPKUNDNR");
                         VKPKUNDNR.Value = row.VKPKUNDNR;
                          f.Add(VKPKUNDNR);
                        XElement VKPARTST = new XElement("VKPARTST");
                         VKPARTST.Value = row.VKPARTST;
                          f.Add(VKPARTST);
                        XElement VKPKOLL = new XElement("VKPKOLL");
                         VKPKOLL.Value = row.VKPKOLL;
                          f.Add(VKPKOLL);
                        XElement VKPART = new XElement("VKPART");
                         VKPART.Value = row.VKPART;
                          f.Add(VKPART);
                        XElement VKPFARBE = new XElement("VKPFARBE");
                         VKPFARBE.Value = row.VKPFARBE;
                          f.Add(VKPFARBE);
                        XElement VKPPREIS = new XElement("VKPPREIS");
                         VKPPREIS.Value = row.VKPPREIS;
                          f.Add(VKPPREIS);
                        XElement VKPMEEH = new XElement("VKPMEEH");
                         VKPMEEH.Value = row.VKPMEEH;
                          f.Add(VKPMEEH);
                        XElement VKPLIEFTERM = new XElement("VKPLIEFTERM");
                         VKPLIEFTERM.Value = row.VKPLIEFTERM;
                          f.Add(VKPLIEFTERM);
                        XElement VKPMENGE1 = new XElement("VKPMENGE1");
                         VKPMENGE1.Value = row.VKPMENGE1;
                          f.Add(VKPMENGE1);
                        XElement VKPPOSWERT = new XElement("VKPPOSWERT");
                         VKPPOSWERT.Value = row.VKPPOSWERT;
                          f.Add(VKPPOSWERT);
                  root.Add(f);
                }
                sw.Write(xdoc);
                sw.Close();
                sw.Dispose();
            }
            catch (Exception ex)
            { 
            }
      
        }
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #region INotifyPropertyChanging Members

        public event PropertyChangingEventHandler PropertyChanging;

        // Used to notify that a property is about to change
        private void NotifyPropertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        #endregion
        public bool IsFirst=false;
        private string _Photo = "YES";
        public string Photo
        {
            get
            {
                return _Photo;
            }
            set
            {
                if (value != _Photo)
                {
                    NotifyPropertyChanging("Photo");
                    _Photo = value;
                    NotifyPropertyChanged("Photo");
                    NotifyPropertyChanged("PhotoISOSource");
                }
            }
        }
        public string PhotoSource
        {
            get
            {
                string image = string.Format("/Data/Images/{0}", "YES");
                if (!Photo.Contains("http://"))
                {
                    return string.Format("/Data/Images/{0}", Photo);
                }
                return Photo;
            }
        }
    public BitmapImage PhotoISOSource
        {
            get
            {
                string image = string.Format("Data/PhoneDB/Pictures/{0}.jpg",Photo);
                BitmapImage bi = new BitmapImage();
                IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();
                if(!iso.FileExists(image))
                    image = string.Format("Data/PhoneDB/Pictures/{0}.jpg", "YES");
                using (IsolatedStorageFileStream fileStream = iso.OpenFile(image, FileMode.Open, FileAccess.Read))
                {
                    //bi.SetSource(fileStream);
                }              
                return bi;
            }
        }                                        
    }
}
